<?php
class fwDbEngin
{
	public static $dieOnError = true;
	public static $error = null;
	
	/** SGDB Host (IP or Name)
	 *  @var string */
	public $host='';
	
	/** SGDB Login
	 *  @var string */
	public $login='';
	
	/** SGDB Password
	 *  @var string */
	public $password='';
	
	/** SGDB name
	 *  @var string */
	public $database='';
	
	/** Connector
	 *  @var int */
	private $link=false;

	
	/**
	 * @param string $host SGDB Host
	 * @param string $login SGDB Login
	 * @param string $password SGDB Password
	 * @param string $database Database
	 */
	public function __construct($host='', $login='', $password='', $database='')
	{
		$this->host = $host;
		$this->login = $login;
		$this->password = $password;
		$this->database = $database;
	}
	
	/**
	 * Open a connection to the server
	 *
	 * @return int|false
	 */
	private function connect()
	{
		if($this->link !== false) return $this->link;
		if ($this->link=mysql_pconnect($this->host,$this->login,$this->password))
		{
			if ($db_selected = mysql_select_db($this->database, $this->link))
			{
				if(strtoupper(mysql_client_encoding($this->link)) != 'UTF8')
				{
					mysql_query('SET NAMES \'UTF8\';');
					mysql_query('SET CHARACTER SET  \'UTF8\';');
				}
				return $this->link;
			}
			else
			{
				//@@@
				//fwMsgMessageCollection::addMessage(new fwMsgBddError("{BDD_ERROR_CONNECT_BASE}"));
				return false;
			}
		}
		else
		{
			//@@@
			//fwMsgMessageCollection::addMessage(new fwMsgBddError("{BDD_ERROR_CONNECTION}"));
			return false;
		}
	}

	/**
	 * Gets the list of fields
	 *
	 * @param string $table_name name of the table
	 * @return array|false
	 */
	public function getFields($table_name)
	{
		if($table_name=="")
		{
			return false;
		}
		$sql = "SHOW COLUMNS FROM `".$table_name."`";
		return $this->getData($sql);
	}

	/**
	 * Gets the list of tables
	 *
	 * @return array
	 */
	public function getTables()
	{
		$sql = "SHOW TABLES FROM ".$this->database;
		return $this->getData($sql);
	}

	/**
	 * Gets the list of indexes
	 *
	 * @param string $table_name nom de la table
	 * @return array|false
	 */
	public function getIndexes($table_name)
	{
		if($table_name=="")
		{
			return false;
		}
		$sql = "SHOW INDEX FROM `".$table_name."`";
		return $this->getData($sql);
	}

	/**
	 * To retreive the contents of a query
	 *
	 * @param string $sql_query query to execute
	 * @return array|false
	 */
	public function getData($sql_query, $result_type=MYSQL_ASSOC)
	{
		$this->connect();

		if ($result_data=mysql_query($sql_query,$this->link))
		{
			$datas = array();
			while($d = mysql_fetch_array($result_data, $result_type))
			{
				$datas[]=$d;
			}
			mysql_free_result($result_data);
			return $datas;
		}
		else
		{
			//@@@
			//fwMsgMessageCollection::addMessage(new fwMsgBddError(mysql_error(), $sql_query));
			if(self::$dieOnError)
			{
				die('ERROR SQL : '.mysql_error($this->link).'\n\n\n'.$sql_query);
			}
			else
			{
				self::$error = mysql_error($this->link);
			}
			return false;
		}
	}

	/**
	 * To retreive the first row of a query
	 *
	 * @param string $sql_query query to execute
	 * @return array|false
	 */
	public function getRow($sql_query, $result_type=MYSQL_ASSOC)
	{
		$this->connect();

		if ($result_data=mysql_query($sql_query,$this->link))
		{
			if($data = mysql_fetch_array($result_data, $result_type))
			{
				mysql_free_result($result_data);
				return $data;
			}
			else
			{
				mysql_free_result($result_data);
				return false;
			}
		}
		else
		{
			//@@@
			//fwMsgMessageCollection::addMessage(new fwMsgBddError(mysql_error(), $sql_query));
			//die(fwMsgMessageCollection::getMessageHTML());
			if(self::$dieOnError)
			{
				die('ERROR SQL : '.mysql_error($this->link).'\n\n\n'.$sql_query);
			}
			else
			{
				self::$error = mysql_error($this->link);
			}
			return false;
		}
	}

	/**
	 * To retreive the first cell of the first row of a query
	 *
	 * @param string $sql_query query to execute
	 * @return array array who conains all the fields of the one row
	 */
	public function getCell($sql_query)
	{
		$data = $this->getRow($sql_query, MYSQL_NUM);
		if($data!==false)
		{
			return $data[0];
		}
		else
		{
			return false;
		}
	}

	/**
	 * Sends an SQL query to MySQL (INSERT, UPDATE, DELETE...)
	 *
	 * @param string $sql_query query to execute
	 * @return int
	 */
	public function setData($sql_query)
	{
		$this->connect();

		if ($result_data=mysql_query($sql_query,$this->link))
		{
			return $result_data;
		}
		else
		{
			//@@@
			//fwMsgMessageCollection::addMessage(new fwMsgBddError(mysql_error(), $sql_query));
			//die(fwMsgMessageCollection::getMessageHTML());
			if(self::$dieOnError)
			{
				die('ERROR SQL : '.mysql_error($this->link).'\n\n\n'.$sql_query);
			}
			else
			{
				self::$error = mysql_error($this->link);
			}
			return false;
		}
	}

	/**
	 * Gets  the ID generated from the previous INSERT operation
	 *
	 * @return int
	 */
	public function insertedId()
	{
		return mysql_insert_id($this->link);
	}
}
?>